package com.fsnbigdata.repository;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.Organism;
import com.fsnbigdata.domain.QOrganism;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = OrganismRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = OrganismRepositoryCustom.class)
@Transactional(readOnly = true)
public class OrganismRepositoryImpl extends QueryDslRepositorySupportExt<Organism> implements OrganismRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    OrganismRepositoryImpl() {
        super(Organism.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ENDEMISM = "endemism";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CHARACTERISTIC = "characteristic";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LATIN_NAME = "latinName";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String USE_VALUES = "useValues";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ALIAS = "alias";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String HABITAT = "habitat";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String INITIAL = "initial";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SPECIMEN_SUMMARY = "specimenSummary";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAMED_INFO = "namedInfo";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    @Override
    public Page<Organism> findAll(Predicate predicate, Pageable pageable) {
        QOrganism organism = QOrganism.organism;
        JPQLQuery<Organism> query = from(organism).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable, organism);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Organism> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QOrganism organism = QOrganism.organism;
        JPQLQuery<Organism> query = from(organism);
        if(creator != null ){
            query.where(organism.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { organism.name, organism.latinName, organism.initial, organism.alias, organism.namedInfo, organism.habitat, organism.endemism, organism.characteristic, organism.useValues, organism.specimenSummary, organism.creator };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, organism.name).map(LATIN_NAME, organism.latinName).map(INITIAL, organism.initial).map(ALIAS, organism.alias).map(NAMED_INFO, organism.namedInfo).map(HABITAT, organism.habitat).map(ENDEMISM, organism.endemism).map(CHARACTERISTIC, organism.characteristic).map(USE_VALUES, organism.useValues).map(SPECIMEN_SUMMARY, organism.specimenSummary).map(CREATOR, organism.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organism);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Organism> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QOrganism organism = QOrganism.organism;
        JPQLQuery<Organism> query = from(organism);
        Path<?>[] paths = new Path<?>[] { organism.name, organism.latinName, organism.initial, organism.alias, organism.namedInfo, organism.habitat, organism.endemism, organism.characteristic, organism.useValues, organism.specimenSummary, organism.creator };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(organism.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, organism.name).map(LATIN_NAME, organism.latinName).map(INITIAL, organism.initial).map(ALIAS, organism.alias).map(NAMED_INFO, organism.namedInfo).map(HABITAT, organism.habitat).map(ENDEMISM, organism.endemism).map(CHARACTERISTIC, organism.characteristic).map(USE_VALUES, organism.useValues).map(SPECIMEN_SUMMARY, organism.specimenSummary).map(CREATOR, organism.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, organism);
    }
}
